"use strict";

const BaseController = require("./base");
const BaiduApiDto = require("../dto/baidu-api.dto");
const tokenKey = "qsweb-baiduAi-token-redis";
class BaiDuApiController extends BaseController {
  /**
   * @description 获取通过百度飞桨时需要的accss_token
   * @method GET
   * @author jacinto
   */
  async stsToken() {
    try {
      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.getAccessToken);
      if (!verifytRes.success) throw verifytRes.msg;

      console.log('參數是:')
      console.log(verifytRes.params)

      //获取到token
      const res = await this.service.baiduapi.getStsToken(verifytRes.params);
      if (!res.success) throw res.msg;
      await this.ctx.helper.setRedis(
        tokenKey,
        res.data,
        //这个过期时间暂时和oss一样
        this.config.oss.tokenExpireTime
      ); // 获取token成功，缓存到redis
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }
  }


  /**
   * 图像分类
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async picCategory() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.picCategory);
      if (!verifytRes.success) throw verifytRes.msg;

      //如果没有传递url也没有上传图片，那么直接抛出异常
      if (!verifytRes.params.image && !verifytRes.params.input_type) {
        throw '请上传图片或者是url'
      }

      //前往service进行操作
      const res = await this.service.baiduapi.picCategory(verifytRes.params, result.access_token);
      await this.success(res.data, "success");

    } catch (err) {
      await this.error(err);
    }
  }



  /**
   * 图像集列表
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async list_datasets() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.listDatasets);
      if (!verifytRes.success) throw verifytRes.msg;

      //前往service进行操作
      const res = await this.service.baiduapi.list_datasets(verifytRes.params, result.access_token);
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }
  }



  /**
   * 创建图像集
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async create_dataset() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.createDataset);
      if (!verifytRes.success) throw verifytRes.msg;

      //前往service进行操作
      const res = await this.service.baiduapi.create_dataset(verifytRes.params, result.access_token);
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }

  }



  /**
   * 删除图像集
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async delete_dataset() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.deleteDataset);
      if (!verifytRes.success) throw verifytRes.msg;

      //前往service进行操作
      const res = await this.service.baiduapi.delete_dataset(verifytRes.params, result.access_token);
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }

  }



  /**
   * 标签列表
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async list_categories() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.listCategories);
      if (!verifytRes.success) throw verifytRes.msg;

      //前往service进行操作
      const res = await this.service.baiduapi.list_categories(verifytRes.params, result.access_token);
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }
  }


  /**
   * 删除标签
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async delete_category() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.deleteCategory);
      if (!verifytRes.success) throw verifytRes.msg;

      //前往service进行操作
      const res = await this.service.baiduapi.delete_category(verifytRes.params, result.access_token);
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }
  }



  /**
   * 添加数据
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async add_data() {
    try {
      const result = await this.ctx.helper.getRedis(tokenKey)
      //如果不存在access_token
      if (result === null) {
        throw '请先获取access_token'
      }

      //进行参数验证
      const verifytRes = this.verifytParams(BaiduApiDto.addData);
      if (!verifytRes.success) throw verifytRes.msg;

      //前往service进行操作
      const res = await this.service.baiduapi.add_data(verifytRes.params, result.access_token);
      await this.success(res.data, "success");
    } catch (err) {
      await this.error(err);
    }
  }
}

module.exports = BaiDuApiController;
